<!-- 头部导航组件 -->
<template>
    <ul class="app-header-nav">
        <li class="home"><RouterLink to="/">首页</RouterLink></li>
        <li v-for="item in list" :key="item.id" @mousemove="show(item)" @mouseleave="hidden(item)">
            <router-link :to="`/category/${item.id}`" @click="hidden(item)">{{item.name}}</router-link >
            <div class="layer" :class="{open: item.open}">
              <ul>
                <li v-for="sub in item.children" :key="sub.id">
                <RouterLink :to="`/category/sub/${sub.id}`" @click="hidden(item)">
                  <img :src="sub.picture" alt="">
                  <p>{{sub.name}}</p>
                </RouterLink>
              </li>
            </ul>
            </div>
        </li>
    </ul>
</template>

<script>
import { computed } from '@vue/runtime-core'
import { useStore } from 'vuex'
export default {
    name: 'AppHeaderNav',
    setup () {
      const store = useStore ()
      //响应式数据，拿vuex中的数据需要用计算属性
      const list = computed(() => {
        return store.state.category.list
      })
      //调用store中的显示隐藏方法
      const show = (item) => {
        store.commit('category/show', item.id)
      }
      const hidden = (item) => {
        store.commit('category/hidden', item.id)
      }
      return {list,hidden,show}
    }

}
</script>

<style lang="less" scoped>
.app-header-nav {
  width: 820px;
  display: flex;
  padding-left: 40px;
  position: relative;
  z-index: 998;
  > li {
    margin-right: 40px;
    width: 38px;
    text-align: center;
    > a {
      font-size: 16px;
      line-height: 32px;
      height: 32px;
      display: inline-block;
    }
    &:hover {
      > a {
        color: @xtxColor;
        border-bottom: 1px solid @xtxColor;
      }
    }
  }
}
.layer {
  &.open {
    height: 132px;
    opacity: 1;
  };
  width: 1240px;
  background-color: #fff;
  position: absolute;
  left: -200px;
  top: 56px;
  height: 0;
  overflow: hidden;
  opacity: 0;
  box-shadow: 0 0 5px #ccc;
  transition: all .2s .1s;
  ul {
    display: flex;
    flex-wrap: wrap;
    padding: 0 70px;
    align-items: center;
    height: 132px;
    li {
      width: 110px;
      text-align: center;
      img {
        width: 60px;
        height: 60px;
      }
      p {
        padding-top: 10px;
      }
      &:hover {
        p {
          color: @xtxColor;
        }
      }
    }
  }
}

</style>